package jp.gr.jted;

import java.util.ArrayList;

import jp.gr.jted.bean.Talk;
import jp.gr.jted.bean.ViewHolder;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TalkAdapter extends ArrayAdapter<Object>{

	@SuppressWarnings({ "rawtypes" })
	private ArrayList list;
	private LayoutInflater inflater;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public TalkAdapter(Context context, int textViewResourceId, ArrayList list) {
		super(context, textViewResourceId, list);
		this.list = list;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override  
	public View getView(int position, View convertView, ViewGroup parent){
		ViewHolder holder;
		Talk talk = (Talk)list.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.talk_row, parent, false);
			holder = new ViewHolder();
			convertView.setTag(holder);
			// Thumnail
			ImageView thumnail = (ImageView) convertView.findViewById(R.id.talk_thumnail);
			holder.setThumnail(thumnail);
			// Title
			TextView talkTitle = (TextView) convertView.findViewById(R.id.talk_title);
			holder.setTitle(talkTitle);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}

		holder.getTitle().setText(talk.getTitle());
		holder.getThumnail().setImageBitmap((Bitmap) talk.getBitmap());

		return convertView;
	}
}
